﻿CREATE FUNCTION [ODCore].[GetParentFeatureID]  (@childhierarchyid hierarchyid)
RETURNS int
AS
BEGIN

DECLARE @parenthierarchyid hierarchyid, @parentFeatureID int;
IF @childhierarchyid.GetLevel()>1
BEGIN
SET @parenthierarchyid = @childhierarchyid.GetAncestor(1);
END
ELSE
BEGIN
SET @parenthierarchyid = @childhierarchyid;
END
SELECT @parentFeatureID=FeatureID from ODCore.Feature where [HierarchyCode]=@parenthierarchyid;

   RETURN(@parentFeatureID)
END
